package cn.echo.consul.manager.config;

import cn.echo.consul.manager.interceptor.AuthenticationInterceptor;
import cn.echo.consul.manager.interceptor.PermissionInterceptor;
import cn.echo.consul.manager.service.AccessTokenService;
import cn.echo.consul.manager.service.ProjectInfoService;
import cn.echo.consul.manager.service.UserInfoService;
import cn.echo.consul.manager.util.JsonUtil;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * WebMvc配置
 * @author lonyee
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    AccessTokenService accessTokenService;
    @Resource
    UserInfoService userInfoService;
    @Resource
    ProjectInfoService projectInfoService;

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(JsonUtil.getInstance());
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(0, stringHttpMessageConverter);
        converters.add(1, jackson2HttpMessageConverter);
    }

    /**加入拦截器 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor(accessTokenService))
                .excludePathPatterns("/api/user/login")
                .addPathPatterns("/api/**");
        registry.addInterceptor(new PermissionInterceptor(userInfoService, projectInfoService))
                .excludePathPatterns("/api/user/login")
                .addPathPatterns("/api/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        //WebMvcConfigurer.super.addResourceHandlers(registry);
    }


    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return factory -> {
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
            factory.addErrorPages(error404Page);
        };
    }
}
